package com.xsp.exception;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xsp
 * @date 2024/2/20
 * @desc 自定义全局异常处理器
 */
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        MyException myException = null;
        // 判断不同的异常类型
        if (e instanceof MyException) {
            // 自定义异常
            myException = (MyException) e;
        } else {
            myException = new MyException("FBI 异常提示！");
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", myException.getMessage());
        mv.setViewName("500");
        return null;
    }
}
